import * as ts from "typescript";
const fs = require('fs');
var path  = require('path');  // node内置模块


function getRootActionName(tsOrJsFilePath:string) {
  let actionNameList:any = [];

  const sourceFile = ts.createSourceFile(
		tsOrJsFilePath,
		fs.readFileSync(tsOrJsFilePath).toString(),
		ts.ScriptTarget.ES2015,
		/*setParentNodes */ true
	);
  // console.log("🚀 ~ file: testAst.ts:13 ~ getRootActionName ~ sourceFile", sourceFile);

  let varStatementList = sourceFile.statements.filter((astItem:any)=>{
    return astItem.kind === ts.SyntaxKind.VariableStatement;
  });

  let rootStore:any = varStatementList.filter((astItem:any)=>{
    return astItem.getText().includes('...baseActions');
  })[0];

  let actionObj = rootStore?.declarationList?.declarations[0]?.initializer?.arguments[0]?.properties?.filter((astItem:any) => {
     return astItem.name.escapedText === 'actions';
  })[0];


  actionObj.initializer.properties.forEach((node:any) => {
    if(node.kind == ts.SyntaxKind.SpreadAssignment && node.getText() == '...baseActions'){
      actionNameList.push(100);
      actionNameList.push(200);
      actionNameList.push(300);
    }
    if(node.kind ==  ts.SyntaxKind.ShorthandPropertyAssignment){
      actionNameList.push(node.name.escapedText);
    }

    if(node.kind ==  ts.SyntaxKind.MethodDeclaration){
      actionNameList.push(node.name.escapedText);
    }

    if(node.kind == ts.SyntaxKind.PropertyAssignment && node.initializer.kind == ts.SyntaxKind.FunctionExpression){
      actionNameList.push(node.name.escapedText);
    }

    console.log("actionNameList--------------->",actionNameList);

  });


  console.log("🚀 ~ file: testAst.ts:19 ~ getRootActionName ~ actionNameList", actionNameList);

}

var fullPath = path.join(__dirname, "./store/index.ts");

getRootActionName(fullPath);